/**
 * Package: Sound
 * 
 * Requires: System, Timers
 */

#define Tone_C1   440
#define Tone_Cis1 413
#define Tone_D1   392
#define Tone_Dis1 371
#define Tone_E1   350
#define Tone_F1   330
#define Tone_Fis1 312
#define Tone_G1   294
#define Tone_Gis1 277
#define Tone_A1   262
#define Tone_Ais1 247
#define Tone_H1   234
#define Tone_C2   220
#define Tone_Cis2 208
#define Tone_D2   196
#define Tone_Dis2 185
#define Tone_E2   175
#define Tone_F2   165
#define Tone_Fis2 156
#define Tone_G2   147
#define Tone_Gis2 138
#define Tone_A2   131
#define Tone_Ais2 125
#define Tone_H2   117

#define SOUND_INPUT_PORT 3

/// <summary>Plays 'tone' for 'period'.</summary>
/// <param name="tone">The tone to play.</param>
/// <param name="period">How long the tone should be played.</param>
void Sound_PlayTone(word tone, word toneLength)
{
    Timer_T0FRQ(tone, PS_64);
    AbsDelay(toneLength);
    Timer_T0Stop();
}

/// <summary>Plays music using two arrays.</summary>
/// <param name="tones">The tones that should be played.</param>
/// <param name="toneLengths">The length of the tones.</param>
void Sound_PlaySong(word tones[], word toneLengths[])
{
    word i; i = 0;

    while (tones[i])
    {
        Sound_PlayTone(tones[i], toneLengths[i]);

        i++;
    }
}

/// <summary>Gets current input sound level.</summary>
/// <returns>The current input sound level.</returns>
word Sound_GetSoundLevel(void)
{
    return ReadAdc(SOUND_INPUT_PORT);
}